!function(){
	function formResult(msg, state) {
        layer.closeAll();
        if(state == 0) {
            layer.alert(msg, {icon: 5});
        }else{
            layer.alert(msg, {icon: 1},function(){
                location.reload(true);
            });
        }
    }
    function checkparam(){
        $('input[type="submit"]').click(function(e){
            e.stopPropagation();
        });
        $('.table tr').not('.lastTr').on('click',function(){
            var $choose = $(this).find('.chooseBtn');
            var $this = $(this);
            var member_id = parent.getMember_id(),
                finance_type = 2, //疗程卡
                consume_name = $this.find('.name input[type=text]').val(),
                sell_price = $choose.attr('data-price')||0,
                really_price = $choose.attr('data-price')||0,
                count = 1,
                relation_id = $choose.attr('data-id');
            console.log(consume_name);
            if (typeof member_id == 'undefined') {
                layer.alert('请选择会员！', {icon: 5});
                return false;
            }else if(!$this.find('input').hasClass('read-only')){
                return false;
            } else {
                $.ajax({
                    type:'POST',
                    dataType:'json',
                    url:'/business/financial/cashier?action=chooseproduct',
                    data:{member_id:member_id,finance_type:finance_type,consume_name:consume_name,sell_price:sell_price,really_price:really_price,count:count,relation_id:relation_id},
                    success:function (data){
                        if(data.state==1){
                            $.ajax({
                                type:'POST',
                                dataType:'json',
                                url:'/business/financial/cashier?action=productlist',
                                data:{member_id:member_id},
                                success:function (data){

                                    if(data.state==1){
                                        var index = parent.layer.getFrameIndex(window.name);
                                        var html = '';
                                        function trans(data) {
                                            var $type;
                                            switch (data) {
                                                case '1':
                                                    $type = '储值卡';
                                                    break;
                                                case '2':
                                                    $type = '疗程卡';
                                                    break;
                                                case '3':
                                                    $type = '家居产品';
                                                    break;
                                                case '4':
                                                    $type = '合作项目';
                                                    break;
                                                case '5':
                                                    $type = '服务项目';
                                                    break;
                                            }
                                            return $type;
                                        }

                                        var arrSum = [];
                                        parent.$('.table tbody').empty();
                                        $.each(data.result,function(i,o){
                                            arrSum.push(o.really_price * o.count);
                                            html = '<tr>' +
                                                '<form>' +
                                                '<td class="types"><i>'+ trans(o.finance_type)+'</i><span>充值金额:'+ o.recharge+'</span><span>赠送金额:'+ o.give_balance+'</span></td>' +
                                                '<td>'+ o.consume_name+'</td>' +
                                                '<td>'+ o.sell_price+'</td>' +
                                                '<td><input class="calc reaPrice" type="text" value="'+o.really_price+'"/></td>' +
                                                '<td><input class="calc count" type="text" value="'+ o.count +'"></td>' +
                                                '<td>'+ o.really_price * o.count+'</td>' +
                                                '<td><a data-nid="'+i+'" class="add-member btn btn-sm" href="javascript:;" onclick="openP('+"'/business/financial/cashier?action=stafflist'"+','+ i +','+ o.finance_id+')">'+parent.defaultVal(o.employee_name)+'</a></td>' +
                                                '<td><input class="saleFeat" type="text" value="'+ parent.defaultReward(o.reward) +'"></td>' +
                                                '<td><input class="but_set_state btn btn-sm btn-borB " type="submit" value="删除" durl="/business/financial/cashier?action=delproduct" did="'+ o.finance_id+'" mid="'+ o.member_id+'"/></td>' +
                                                '</form>'+
                                                '</tr>';

                                            parent.$('.table tbody').append(html);

                                        });
                                        var allMoney = arrSum.reduce(function (x, y) {
                                            return x + y;
                                        });
                                        parent.$('.table .types').each(function(){
                                            if($(this).find('i').text() !=='储值卡'){
                                                $(this).find('span').hide();
                                            }
                                        });
                                        parent.$('.table tbody').append('<tr>' +
                                            '<td colspan="1" style="border-right: 0;">总计金额：' +
                                            '<em class="red">￥<span class="total-money">'+allMoney+'</span></em>' +
                                            '</td>' +
                                            '<td class="text-right" colspan="8" style="border-left: 0;">' +
                                            '<em class="red">提示：</em>' +
                                            '会员有多次服务消费的情况，请选择疗程卡；会员单次项目服务消费的情况，请选择有服务项目会员消费后，无需提供后续服务，请选择无服务项目' +
                                            '</td>' +
                                            '</tr>');
                                        parent.$('.table').parent().find('.clearingBtn').removeClass('hide');

                                        parent.layer.close(index);

                                    }else{
                                        layer.alert(data.message, {icon: 5});
                                    }
                                }
                            })
                        }else{
                            layer.alert(data.message, {icon: 5});
                        }
                    }
                })
            }
        })
    }
    $(function() {
        window.formResult = formResult;
        checkparam();
    })
}();